import { createWebHashHistory, createRouter, RouteRecordRaw } from 'vue-router'
import { AclRole } from '@/auth/auth.enum'
import { authStore } from '@/store/auth-store'
import { getSession } from '@/auth/auth.service'

// 配置说明
// meta:
//      title           需要展示的模块标题
//      icon            对应的图标
//      enableRoles     有哪些角色能访问
//      notShowChild    是否需要展示子路由 - 默认展示  不需要展示的是比如当前模块有详情页，请参照【六道轮回】

const routes: Array<RouteRecordRaw> = [
    {
        path: '/:pathMatch(.*)*',
        redirect: '/404'
    },
    {
        path: '/',
        redirect: '/layout/home'
    },
    {
        path: '/404',
        name: '404',
        component: () => import('../views/notFound/index.vue')
    },
    {
        path: '/notauth',
        name: 'NotAuth',
        component: () => import('../views/notAuth/index.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/login/index.vue'),
    },
    {
        path: '/test',
        name: 'Test',
        component: () => import('../views/test/index.vue'),
        meta: { title: "测试专用", enableRoles: [AclRole.ADMIN, AclRole.LAGC, AclRole.SDYW, AclRole.WFGD, AclRole.YSGC] },
    },
    {
        path: '/layout',
        name: 'Layout',
        component: () => import('../components/layout/index.vue'),
        redirect: '/layout/home',
        meta: { title: "首页", enableRoles: [AclRole.ADMIN, AclRole.LAGC, AclRole.SDYW, AclRole.WFGD, AclRole.YSGC] },
        children: [
            {
                path: '/layout/home',
                name: 'Home',
                component: () => import('../views/home/index.vue'),
                meta: { title: "工作台", icon: 'Monitor', enableRoles: [AclRole.ADMIN, AclRole.LAGC, AclRole.SDYW, AclRole.WFGD, AclRole.YSGC] }
            },
            {
                path: '/layout/nether',
                name: 'Nether',
                component: () => import('../views/nether/index.vue'),
                redirect: '/layout/nether/job',
                meta: { title: "阴曹地府", icon: 'HomeFilled', enableRoles: [AclRole.ADMIN, AclRole.LAGC, AclRole.SDYW, AclRole.WFGD, AclRole.YSGC] },
                children: [
                    {
                        path: '/layout/nether/job',
                        name: 'NetherJob',
                        component: () => import('../views/nether/job.vue'),
                        meta: { title: "职位表", icon: '', enableRoles: [AclRole.ADMIN, AclRole.LAGC, AclRole.SDYW, AclRole.WFGD, AclRole.YSGC] },
                    },
                    {
                        path: '/layout/nether/admin',
                        name: 'NetherAdmin',
                        component: () => import('../views/nether/admin.vue'),
                        meta: { title: "管理表", icon: '', enableRoles: [AclRole.ADMIN, AclRole.LAGC, AclRole.SDYW, AclRole.WFGD, AclRole.YSGC] },
                    },
                    {
                        path: '/layout/nether/flowpath',
                        name: 'NetherFlowpath',
                        component: () => import('../views/nether/flowpath.vue'),
                        meta: { title: "十三站", icon: '', enableRoles: [AclRole.ADMIN, AclRole.LAGC, AclRole.SDYW, AclRole.WFGD, AclRole.YSGC] },
                    }
                ]
            },
            {
                path: '/layout/obituary',
                name: 'Obituary',
                component: () => import('../views/obituary/index.vue'),
                redirect: '/layout/obituary/introduct',
                meta: { title: "生死簿", icon: 'Reading', enableRoles: [AclRole.ADMIN, AclRole.SDYW, AclRole.LAGC] },
                children: [
                    {
                        path: '/layout/obituary/introduct',
                        name: 'ObituaryIntroduct',
                        component: () => import('../views/obituary/introduct.vue'),
                        meta: { title: "简介", icon: '', enableRoles: [AclRole.ADMIN, AclRole.SDYW, AclRole.LAGC] },
                    },
                    {
                        path: '/layout/obituary/list',
                        name: 'ObituaryList',
                        component: () => import('../views/obituary/list.vue'),
                        meta: { title: "万物生灵", icon: '', enableRoles: [AclRole.ADMIN, AclRole.SDYW, AclRole.LAGC] },
                    }
                ]
            },
            {
                path: '/layout/soul',
                name: 'Soul',
                component: () => import('../views/soul/index.vue'),
                meta: { title: "勾魂管理", icon: 'FolderChecked', enableRoles: [AclRole.ADMIN, AclRole.SDYW, AclRole.LAGC, AclRole.YSGC] }
            },
            {
                path: '/layout/trial',
                name: 'Trial',
                component: () => import('../views/trial/index.vue'),
                meta: { title: "审判记录", icon: 'AlarmClock', enableRoles: [AclRole.ADMIN, AclRole.SDYW, AclRole.LAGC] },
            },
            {
                path: '/layout/infernal',
                name: 'Infernal',
                component: () => import('../views/infernal/index.vue'),
                redirect: '/layout/infernal/work',
                meta: { title: "十八层地狱", icon: 'OfficeBuilding', enableRoles: [AclRole.ADMIN, AclRole.LAGC, AclRole.SDYW, AclRole.WFGD, AclRole.YSGC] },
                children: [
                    {
                        path: '/layout/infernal/work',
                        name: 'InfernalWork',
                        component: () => import('../views/infernal/work.vue'),
                        meta: { title: "地狱说明", icon: '', enableRoles: [AclRole.ADMIN, AclRole.LAGC, AclRole.SDYW, AclRole.WFGD, AclRole.YSGC] },
                    },{
                        path: '/layout/infernal/user',
                        name: 'InfernalUser',
                        component: () => import('../views/infernal/user.vue'),
                        meta: { title: "地狱管理", icon: '', enableRoles: [AclRole.ADMIN, AclRole.LAGC, AclRole.SDYW, AclRole.WFGD, AclRole.YSGC] },
                    },{
                        path: '/layout/infernal/device',
                        name: 'InfernalDevice',
                        component: () => import('../views/infernal/device.vue'),
                        meta: { title: "设备管理", icon: '', enableRoles: [AclRole.ADMIN, AclRole.LAGC, AclRole.SDYW, AclRole.WFGD, AclRole.YSGC] },
                    }
                ]
            },
            {
                path: '/layout/lunhui',
                name: 'Lunhui',
                component: () => import('../views/lunhui/index.vue'),
                redirect: '/layout/lunhui/list',
                meta: { title: "六道轮回", icon: 'Stopwatch', notShowChild: true, enableRoles: [AclRole.ADMIN, AclRole.LAGC] },
                children: [
                    {
                        path: '/layout/lunhui/list',
                        name: 'LunhuiList',
                        component: () => import('../views/lunhui/list.vue'),
                        meta: { title: "轮回列表", icon: '', parentName: 'Lunhui', enableRoles: [AclRole.ADMIN, AclRole.LAGC] },
                    },{
                        path: '/layout/lunhui/detail/:id',
                        name: 'LunhuiDetail',
                        component: () => import('../views/lunhui/detail.vue'),
                        meta: { title: "轮回详情", icon: '', parentName: 'Lunhui', enableRoles: [AclRole.ADMIN, AclRole.LAGC]  }
                    }
                ]
            },
            {
                path: '/layout/GMB',
                name: 'GMB',
                component: () => import('../views/GMB/index.vue'),
                meta: { title: "冥币管理", icon: 'Wallet', enableRoles: [AclRole.ADMIN, AclRole.SDYW] },
            },
            {
                path: '/layout/journal',
                name: 'Journal',
                component: () => import('../views/journal/index.vue'),
                meta: { title: "日志管理", icon: 'Document', enableRoles: [AclRole.ADMIN, AclRole.SDYW] },
            },
            // {
            //     path: '/layout/setup',
            //     name: 'Setup',
            //     component: () => import('../views/setup/index.vue'),
            //     redirect: '/layout/setup/admin',
            //     meta: { title: "设置", icon: 'Setting', enableRoles: [AclRole.ADMIN, AclRole.SDYW] },
            //     children: [{
            //         path: '/layout/setup/role',
            //         name: 'Role',
            //         component: () => import('../views/setup/role.vue'),
            //         meta: { title: "角色权限", icon: '', enableRoles: [AclRole.ADMIN, AclRole.SDYW] },
            //     }]
            // }
        ]
    }
]

const Router = createRouter({
    history: createWebHashHistory(),
    routes
})

Router.beforeEach((to, from, next) => {
    const { token, userinfo } = getSession(['token', 'userinfo']);
    const store = authStore()
    const toName = to.name as string
    // 如果没登录
    if(toName !== 'Login' && !token){
        next({ name: 'Login' })
        return true;
    }
    // 如果去登录页，或者在登录页，就说明没登录
    if(toName === 'Login') {
        next()
        return true
    } 
    // 判断跳转路由是否有权限
    const enableRoles: string[] = to.meta.enableRoles as [] || [];
    if(!['NotAuth', 'Login'].includes(toName) && !enableRoles.includes(userinfo.roleCode)) {
        next({ name: 'NotAuth' })
        return true;
    }else {
        store.updateUserInfo()
        next()
    }
    
    return true
})

export default Router